#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time      :2021/11/3 10:45
# @Author    :cjw
import time

from requests import Response

from use_custom_api.api.feishu_api import FeiShuApi


class Calendar(FeiShuApi):
	def create(self, **kwargs) -> Response:
		"""创建日历"""
		if 'json' in kwargs:
			json_data = kwargs.get('json')
		else:
			json_data = {
				'summary': f'测试日历_{round(time.time() * 1000)}',
				'description': f'使用开放接口创建日历_{round(time.time() * 1000)}',
				'permissions': 'public',
				'color': -1,
				'summary_alias': f'日历备注名_{round(time.time() * 1000)}'
			}
		data = {
			'method': 'POST',
			'url': f'{self.base_url}/calendars',
			'json': json_data
		}
		return self.fs_request(data)

	def update(self, calendar_id: str, **kwargs) -> Response:
		"""
		更新日历
		:param calendar_id:
		:param kwargs:
		:return:
		"""
		if 'json' in kwargs:
			json_data = kwargs.get('json')
		else:
			json_data = {
				'summary': f'测试日历_{round(time.time() * 1000)}',
				'description': f'使用开放接口创建日历_{round(time.time() * 1000)}',
				'permissions': 'public',
				'color': -1,
				'summary_alias': f'日历备注名_{round(time.time() * 1000)}'
			}

		data = {
			'method': 'PATCH',
			'url': f'{self.base_url}/calendars/{calendar_id}',
			'json': json_data
		}
		return self.fs_request(data)

	def delete(self, calendar_id: str) -> Response:
		"""删除日历"""
		data = {
			'method': 'DELETE',
			'url': f'{self.base_url}/calendars/{calendar_id}'
		}
		return self.fs_request(data)

	def get(self, calendar_id: str) -> Response:
		"""获取日历"""
		data = {
			'method': 'GET',
			'url': f'{self.base_url}/calendars/{calendar_id}',
		}
		return self.fs_request(data)

	def get_list(self, **kwargs) -> Response:
		"""获取日历列表"""
		if 'json' in kwargs:
			json_data = kwargs.get('json')
		else:
			json_data = {
				'page_size': 50
			}

		data = {
			'method': 'GET',
			'url': f'{self.base_url}/calendars',
			'params': json_data
		}
		return self.fs_request(data)

	def search(self, query: str, **kwargs) -> Response:
		"""搜索日历"""
		if 'json' in kwargs:
			json_data = kwargs.get('json')
		else:
			json_data = {
				'query': query
			}

		data = {
			'method': 'POST',
			'url': f'{self.base_url}/calendars/search',
			'json': json_data
		}
		return self.fs_request(data)

	def subscribe(self, calendar_id: str) -> Response:
		"""订阅日历"""
		data = {
			'method': 'POST',
			'url': f'{self.base_url}/calendars/{calendar_id}/subscribe'
		}
		return self.fs_request(data)

	def unsubscribe(self, calendar_id: str) -> Response:
		"""取消订阅日历"""
		data = {
			'method': 'POST',
			'url': f'{self.base_url}/calendars/{calendar_id}/unsubscribe'
		}
		return self.fs_request(data)
